package com.iamteer.contact.servlet;

import java.io.IOException;
import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.iamteer.contact.dao.impl.ContactDaoImpl;
import com.iamteer.contact.entity.Contact;
/**
 * Servlet implementation class UpdateContact
 */
@WebServlet("/UpdateContact")
public class UpdateContact extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UpdateContact() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		ContactDaoImpl dao = new ContactDaoImpl();
		Contact contact = new Contact();
		Enumeration<String> parameterNames = request.getParameterNames();
		while (parameterNames.hasMoreElements()) {
			String name = (String) parameterNames.nextElement();
			String parameter = request.getParameter(name);
			setContact(contact, name, parameter);
		}
		dao.updateContact(contact);
		response.sendRedirect(request.getContextPath() + "/ListContact");
	}
	private void setContact(Contact contact, String name, String parameter) {
		
		if ("id".equals(name)) {
			contact.setId(parameter);
		} else if ("name".equals(name)) {
			contact.setName(parameter);
		} else if ("gender".equals(name)) {
			contact.setGender(parameter);
		} else if ("age".equals(name)) {
			contact.setAge(Integer.parseInt(parameter));
		} else if ("phone".equals(name)) {
			contact.setPhone(parameter);
		} else if ("email".equals(name)) {
			contact.setEmail(parameter);
		} else if ("qq".equals(name)) {
			contact.setQq(parameter);
		}
	}
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
